package com.qmth.gkwb.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qmth.gkwb.data.constant.OrderStatus;
import com.qmth.gkwb.data.constant.SignStatus;
import com.qmth.gkwb.data.document.BaseDoc;
import com.qmth.gkwb.data.document.Education;
import com.qmth.gkwb.data.document.Family;
import com.qmth.gkwb.data.entity.BKKS;
import com.qmth.gkwb.data.entity.DictSignPoint;
import com.qmth.gkwb.data.entity.DictSubject;
import com.qmth.gkwb.data.document.Order;
import com.qmth.gkwb.data.entity.School;
import com.qmth.gkwb.data.document.Student;
import com.qmth.gkwb.data.exception.CertinoExistException;
import com.qmth.gkwb.data.exception.CommonBusinessException;
import com.qmth.gkwb.data.exception.OrderCreateFailException;
import com.qmth.gkwb.data.exception.SignForbiddenException;
import com.qmth.gkwb.data.pay.OrderService;
import com.qmth.gkwb.data.repository.BKKSRepo;
import com.qmth.gkwb.data.repository.DictSignPointRepo;
import com.qmth.gkwb.data.repository.DictSubjectRepo;
import com.qmth.gkwb.data.repository.OrderRepo;
import com.qmth.gkwb.data.repository.SchoolRepo;
import com.qmth.gkwb.data.repository.StudentRepo;
import com.qmth.gkwb.data.service.StudentService;
import com.qmth.pay.exception.KeyErrorException;

@RestController
@RequestMapping("api/students")
public class StudentApi {

	@Autowired private StudentRepo stuRepo;
	@Autowired private OrderRepo orderRepo;
	@Autowired private StudentService stuService;
	@Autowired private OrderService orderService;
@Autowired private BKKSRepo bkksRepo;
	
	@Autowired private DictSignPointRepo signPointRepo;
	@Autowired private DictSubjectRepo subjectRepo;
	@Autowired private SchoolRepo schoolRepo;
	
	@RequestMapping(method=RequestMethod.GET)
	public List<Student> findAll(){
		return stuRepo.findAll();
	}
	
	@RequestMapping(value="{examId}",method=RequestMethod.GET)
	public Student findByExamId(@PathVariable String examId){
		Student s = null;
		List<Student> list =  stuRepo.findByExamId(examId);
		if(list.size() > 0){
			s = list.get(0);
			if(s.getPay() != null && !s.getPay().isPaid()){
				boolean isPaid = orderService.isOrderPaid(s.getExamId());
				s.getPay().setPaid(isPaid);
			}
		}
		else{
			s = new Student();
			s.setGrade(new BaseDoc(1L,"高三"));
			//s.setSchoolClass(new BaseDoc(1L,"01班"));
			s.setGender(1);
			s.setSportSigned(1);
			s.addEducation(new Education());s.addEducation(new Education());s.addEducation(new Education());
			s.addFamily(new Family());s.addFamily(new Family());
			BKKS ks = bkksRepo.findOne(examId);
			if(ks != null){
				DictSubject ds = ks.getSubjectId() == null?null:subjectRepo.findOne(ks.getSubjectId());
				s.setSubject(new BaseDoc(ds.getId(), ds.getName()));
				DictSignPoint sp = ks.getSchoolId() == null?null:signPointRepo.findOne(ks.getSchoolId());
				s.setSignPoint(new BaseDoc(sp.getId(), sp.getName()));
				School school = ks.getGradSchoolId() == null?null:schoolRepo.findOne(ks.getGradSchoolId());
				if(school != null){
					String regionIdStr = school.getRegionId().toString();
					String cityIdStr = regionIdStr.substring(0, 4);
					s.setGradeSchoolRegion(new BaseDoc(school.getRegionId(),null));
					s.setGradeSchoolCity(new BaseDoc(Long.parseLong(cityIdStr),null));
					s.setGradeSchool(new BaseDoc(school.getId(), school.getName()));
				}
				s.setExamType(new BaseDoc(ks.getExamType().getId(),ks.getExamType().getName()));
				if(ks.getExamType().getId() == 3L){
					s.setForeign(new BaseDoc(1L,"英语"));
				}
				if(ks.getExamType().getId() != 1L){
					s.setSpokenType(new BaseDoc(3L,"不参加考试"));
				}
				s.setSubjectType(ks.getSubjectType());
				Long periodId = ks.getPeriodId();
				s.setPeriodId(periodId);
			}
		}
		return s;
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public ApiRes save(@RequestBody Student student) throws SignForbiddenException,CertinoExistException, 
					OrderCreateFailException, KeyErrorException, CommonBusinessException{
		Student result = 	stuService.apply(student);
		ApiRes res = new ApiRes(String.valueOf(result.getBill()));
		res.setData(result);
		return res;
	}
	
	@RequestMapping(value="orders",method=RequestMethod.GET)
	public List<Order> getStudentOrders(@RequestParam String examId,
			@RequestParam(required=false) OrderStatus status){
		List<Order> orders = null;
		if(status != null){
			orders = orderRepo.findByExamIdAndStatus(examId, status);
		}
		else{
			orders = orderRepo.findByExamId(examId);
		}
		return orders;
	}
	
	
	
}
